package service.majorunit;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import service.notice.NoticeService;
import util.manager.MajorunitManager;
import util.manager.PageManager;
import util.paramvo.SearchParamVO;
import util.returnvo.MajorunitReturnVO;
import util.returnvo.MajorunitSearchListReturnVO;
import vo.MajorunitVO;
import config.PageConfig;
import dao.majorunit.MajorunitDAO;

public class MajorunitServiceImpl implements MajorunitService {

	private MajorunitDAO majorunitDAO;
	private NoticeService noticeService;

	public void setNoticeService(NoticeService noticeService) {
		this.noticeService = noticeService;
	}

	public void setMajorunitDAO(MajorunitDAO majorunitDAO) {
		this.majorunitDAO = majorunitDAO;
	}

	@Override
	public MajorunitReturnVO getMajorunitReturnVO(MajorunitVO param) throws Exception {
		return new MajorunitReturnVO(param.getMajorunit_no(), MajorunitManager.convertUnitToString(param));
	}

	@Override
	public List<MajorunitReturnVO> getMajorunitReturnVOList(List<MajorunitVO> param) throws Exception {
		
		ArrayList<MajorunitReturnVO> list=new ArrayList<MajorunitReturnVO>();
		
		for(MajorunitVO vo : param){
			list.add(getMajorunitReturnVO(vo));
		}
		
		return list;
	}

	@Override
	public MajorunitSearchListReturnVO getPopupSearchedUnit(String search, String page) throws Exception {

		ArrayList<String> searchlist=new ArrayList<String>();
		
		if(search!=null && !search.trim().equals("")){
			StringTokenizer tokenizer=new StringTokenizer(search);
			
			while (tokenizer.hasMoreElements()) {
				searchlist.add(tokenizer.nextToken());
			}
		}
		
		int pageNo=1;
		
		if(page!=null && !page.equals("")) pageNo=Integer.parseInt(page);
		
		List<MajorunitVO> unitlist=majorunitDAO.getSearchedUnitByWord(new SearchParamVO(searchlist, PageConfig.POPUP_ROW_PER_PAGE, pageNo));
		
		return new MajorunitSearchListReturnVO(getMajorunitReturnVOList(unitlist), new PageManager(PageConfig.POPUP_ROW_PER_PAGE, PageConfig.POPUP_PAGE_PER_GROUP, majorunitDAO.countSearchedUnitByWord(searchlist), pageNo));
	}

	@Override
	public MajorunitReturnVO getOneUnitReturn(int majorunit_no) throws Exception {
		return getMajorunitReturnVO(majorunitDAO.getOneUnit(majorunit_no));
	}

	@Override
	public List<String> autoCompleteMajorunitList(String search) throws Exception {

		ArrayList<String> searchlist=new ArrayList<String>();
		
		if(search!=null && !search.trim().equals("")){
			StringTokenizer tokenizer=new StringTokenizer(search);
			
			while (tokenizer.hasMoreElements()) {
				searchlist.add(tokenizer.nextToken());
			}
		}
		
		List<MajorunitVO> unitlist=majorunitDAO.getSearchedUnitByWord(new SearchParamVO(searchlist, PageConfig.AUTOCOMPL_ROW_PER_PAGE, 1));
		
		ArrayList<String> returnlist=new ArrayList<String>();
		
		for(MajorunitVO vo : unitlist){
			returnlist.add(MajorunitManager.convertUnitToString(vo));
		}
		
		return returnlist;
	}
	
	@Override
	public List<MajorunitVO> getMajorunitEditList(String search) throws Exception {
		
		ArrayList<String> searchlist=new ArrayList<String>();
		
		StringTokenizer tokenizer=new StringTokenizer(search);
		
		while (tokenizer.hasMoreElements()) {
			searchlist.add(tokenizer.nextToken());
		}
		
		return majorunitDAO.getSearchedUnitByWord(new SearchParamVO(searchlist, PageConfig.AUTOCOMPL_ROW_PER_PAGE, 1));
	}

	@Override
	public void registerMajorunit(MajorunitVO param) throws Exception {
		majorunitDAO.registerMajorunit(param);
	}

	@Override
	public void updateMajorunit(MajorunitVO param) throws Exception {
		
		int majorunit_no=param.getMajorunit_no();
		
		MajorunitVO oldUnit=majorunitDAO.getOneUnit(majorunit_no);
		
		majorunitDAO.updateMajorunit(param);
		
		MajorunitVO newUnit=majorunitDAO.getOneUnit(majorunit_no);
		
		noticeService.toWriters(
				majorunit_no, 
				getMajorunitReturnVO(oldUnit).getMajorunit_name()+" 변경", 
				"글을 작성하신 "+getMajorunitReturnVO(oldUnit).getMajorunit_name()+"가<br>"+getMajorunitReturnVO(newUnit).getMajorunit_name()+"로 변경되었습니다.");
		
		noticeService.toInterestedMember(
				majorunit_no, 
				getMajorunitReturnVO(oldUnit).getMajorunit_name()+" 변경", 
				"관심학과로 등록하신 "+getMajorunitReturnVO(oldUnit).getMajorunit_name()+"가<br>"+getMajorunitReturnVO(newUnit).getMajorunit_name()+"로 변경되었습니다.");
	}

	@Override
	public void deleteMajorunit(int majorunit_no) throws Exception {
		majorunitDAO.deleteMajorunit(majorunit_no);
		
		noticeService.toWriters(
				majorunit_no, 
				getOneUnitReturn(majorunit_no).getMajorunit_name()+" 폐과", 
				getOneUnitReturn(majorunit_no).getMajorunit_name()+"가 폐과하여 작성하신 글이 노출되지 않게 되었습니다.<br> 마이페이지에서는 열람이 가능합니다.");
		
		noticeService.toInterestedMember(
				majorunit_no, 
				getOneUnitReturn(majorunit_no).getMajorunit_name()+" 폐과", 
				"관심학과로 등록하신 "+getOneUnitReturn(majorunit_no).getMajorunit_name()+"가 폐과하여 접근할 수 없게 되었습니다.");
	}
	
	@Override
	public void deleteMajorunitInUniv(int univ_no) throws Exception {
		majorunitDAO.deleteMajorunitInUniv(univ_no);
	}

	@Override
	public List<MajorunitVO> getMajorunits(int univ_no) throws Exception {
		return majorunitDAO.getMajorunits(univ_no);
	}
}
